package cn.south.campus.business.service.impl;

import cn.south.campus.business.mapper.ExpressTakeOrderMapper;
import cn.south.campus.business.model.entity.ExpressTake;
import cn.south.campus.business.model.entity.ExpressTakeOrder;
import cn.south.campus.business.model.enums.OrderEnums;
import cn.south.campus.business.service.ExpressTakeOrderService;
import cn.south.campus.common.utils.SnowflakeIdWorker;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 *
 * </p>
 *
 * @author chow
 * @since 2025/2/26 14:03
 */
@Service
@AllArgsConstructor
public class ExpressTakeOrderServiceImpl extends ServiceImpl<ExpressTakeOrderMapper, ExpressTakeOrder> implements ExpressTakeOrderService {

    private final ExpressTakeOrderMapper expressTakeOrderMapper;

    @Override
    public String createOrder(ExpressTake expressTake) {
        ExpressTakeOrder order = new ExpressTakeOrder();
        String orderNo = String.valueOf(SnowflakeIdWorker.getId());
        order.setOrderNo(orderNo);
        order.setExpressTakeId(expressTake.getId());
        order.setPrice(expressTake.getPrice());
        order.setPayStatus(OrderEnums.OrderPayStatus.UNPAID.getCode());
        expressTakeOrderMapper.insert(order);
        return orderNo;
    }

    @Override
    public ExpressTakeOrder selectOrderNo(String orderNo) {
        LambdaQueryWrapper<ExpressTakeOrder> wrapper = new LambdaQueryWrapper<ExpressTakeOrder>()
                .eq(ExpressTakeOrder::getOrderNo, orderNo);
        return expressTakeOrderMapper.selectOne(wrapper);
    }

    @Override
    public String delByTakeId(Long id) {
        LambdaQueryWrapper<ExpressTakeOrder> wrapper = new LambdaQueryWrapper<ExpressTakeOrder>()
                .eq(ExpressTakeOrder::getExpressTakeId, id);
        ExpressTakeOrder expressTakeOrder = expressTakeOrderMapper.selectOne(wrapper);
        expressTakeOrder.setStatus(OrderEnums.OrderStatus.CANCEL.getCode());
        expressTakeOrderMapper.updateById(expressTakeOrder);
        return expressTakeOrder.getOrderNo();
    }
}
